/**
 * Date Range Module.
 */
define([
  'jquery',
  'moment',
  'daterangepicker'
], function($, moment, daterangepicker) {
  var DateRange,
    langCurr = VAM.getVar('LANGUAGE');

  DateRange = (function() {
    function DateRange() {
      this.setting = {};
      this.dateRangeOp = function() {};
    }

    DateRange.prototype.settings = {
        defaults: {
          "format": 'YYYY-MM-DD',
          "showDropdowns": true
        },
        dateRangeOp: function(start, end, label) {
            // console.log("A new date range was chosen: " + start.format('YYYY-MM-DD') + ' to ' + end.format('YYYY-MM-DD'));
        }
    };
    DateRange.prototype.momentLocal = function() {
      if(langCurr === 'zh-CN') {
        moment.locale('zh-cn', {
          months: "一月_二月_三月_四月_五月_六月_七月_八月_九月_十月_十一月_十二月".split("_"),
          monthsShort: "1月_2月_3月_4月_5月_6月_7月_8月_9月_10月_11月_12月".split("_"),
          weekdays: "星期日_星期一_星期二_星期三_星期四_星期五_星期六".split("_"),
          weekdaysShort: "周日_周一_周二_周三_周四_周五_周六".split("_"),
          weekdaysMin: "日_一_二_三_四_五_六".split("_"),
          longDateFormat: {
            LT: "Ah点mm",
            LTS: "Ah点m分s秒",
            L: "YYYY-MM-DD",
            LL: "YYYY年MMMD日",
            LLL: "YYYY年MMMD日LT",
            LLLL: "YYYY年MMMD日ddddLT",
            l: "YYYY-MM-DD",
            ll: "YYYY年MMMD日",
            lll: "YYYY年MMMD日LT",
            llll: "YYYY年MMMD日ddddLT"
          },
          meridiemParse: /凌晨|早上|上午|中午|下午|晚上/,
          meridiemHour: function(e, t) {
            return 12 === e && (e = 0), "凌晨" === t || "早上" === t || "上午" === t ? e : "下午" === t || "晚上" === t ? e + 12 : e >= 11 ? e : e + 12
          },
          meridiem: function(e, t) {
            var n = 100 * e + t;
            return 600 > n ? "凌晨" : 900 > n ? "早上" : 1130 > n ? "上午" : 1230 > n ? "中午" : 1800 > n ? "下午" : "晚上"
          },
          calendar: {
            sameDay: function() {
              return 0 === this.minutes() ? "[今天]Ah[点整]" : "[今天]LT"
            },
            nextDay: function() {
              return 0 === this.minutes() ? "[明天]Ah[点整]" : "[明天]LT"
            },
            lastDay: function() {
              return 0 === this.minutes() ? "[昨天]Ah[点整]" : "[昨天]LT"
            },
            nextWeek: function() {
              var e, n;
              return e = t().startOf("week"), n = this.unix() - e.unix() >= 604800 ? "[下]" : "[本]", 0 === this.minutes() ? n + "dddAh点整" : n + "dddAh点mm"
            },
            lastWeek: function() {
              var e, n;
              return e = t().startOf("week"), n = this.unix() < e.unix() ? "[上]" : "[本]", 0 === this.minutes() ? n + "dddAh点整" : n + "dddAh点mm"
            },
            sameElse: "LL"
          },
          ordinalParse: /\d{1,2}(日|月|周)/,
          ordinal: function(e, t) {
            switch (t) {
              case "d":
              case "D":
              case "DDD":
                  return e + "日";
              case "M":
                  return e + "月";
              case "w":
              case "W":
                  return e + "周";
              default:
                  return e
            }
          },
          relativeTime: {
            future: "%s内",
            past: "%s前",
            s: "几秒",
            m: "1分钟",
            mm: "%d分钟",
            h: "1小时",
            hh: "%d小时",
            d: "1天",
            dd: "%d天",
            M: "1个月",
            MM: "%d个月",
            y: "1年",
            yy: "%d年"
          },
          week: {
            dow: 0,
            doy: 4
          }
        });
      } else {
        moment.locale('en');
      }
    };
    DateRange.prototype.init = function(option, callback) {
      this.momentLocal();
      $.extend(true, this.setting, option, this.settings.defaults);
      this.dateRangeOp = callback || this.settings.dateRange;
    };
    DateRange.prototype.dateRange = function($date, option, callback) {
      this.init(option, callback);
      $date.daterangepicker(this.setting, this.dateRangeOp);
    };

    return DateRange;
  })();

  return DateRange;
});
